package com.tianqi.paypal;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Properties;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.tianqi.desktop.MainWindow;

@SuppressWarnings("deprecation")
public class UIPaymentInfoGetter
{
	private static String http_response_str;

	public UIPaymentInfoGetter()
	{
	}

	public static String getPaymentDetailsString (String transaction_id) throws Exception
	{
		HttpPost http_method = new HttpPost(MainWindow.config_properties.getProperty("PAYPAL_URL"));
		String payment_request = "USER=" + MainWindow.config_properties.getProperty("PAYPAL_USERNAME")
				+ "&PWD=" + MainWindow.config_properties.getProperty("PAYPAL_PASSWORD") + "&SIGNATURE="
				+ MainWindow.config_properties.getProperty("PAYPAL_SIGNATURE")
				+ "&METHOD=GetTransactionDetails" + "&TRANSACTIONID="
				+ transaction_id + "&VERSION=94";

		StringEntity entity = new StringEntity(payment_request.toString(),
				"UTF-8");
		entity.setContentType("application/json");
		http_method.setEntity(entity);
		handleHttpMethod(http_method);
		return UIPaymentInfoGetter.http_response_str;
	}
	
	public static Properties getPaymentDetailsProperties (String transaction_id) throws Exception
	{
		return UITransformer.StringToProperties(UIPaymentInfoGetter.getPaymentDetailsString(transaction_id));
	}

	@SuppressWarnings("resource")
	private static void handleHttpMethod(HttpRequestBase http_method)
			throws Exception
	{
		DefaultHttpClient http_client = new DefaultHttpClient();
		http_client = (DefaultHttpClient) wrapClient(http_client);
		HttpResponse http_response = http_client.execute(http_method);

		// ---- int status_code = http_response.getStatusLine().getStatusCode();
		// ---- System.out.println("Status Code: " + status_code);

		InputStream in = http_response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuilder sb = new StringBuilder();
		String line = null;

		while ((line = reader.readLine()) != null)
		{
			sb.append(line + "\n");
		}

		String req_str = sb.toString();
		// System.out.println("Response Str: " + req_str);
		// System.out.println(req_str);
		http_response_str = req_str;

	}

	private static HttpClient wrapClient(HttpClient base)
	{
		try
		{
			SSLContext ctx = SSLContext.getInstance("TLS");

			X509TrustManager tm = new X509TrustManager()
			{

				public void checkClientTrusted(X509Certificate[] xcs,
						String string) throws CertificateException
				{
				}
				
				public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException
				{
				}

				public X509Certificate[] getAcceptedIssuers()
				{
					return null;
				}
			};

			ctx.init(null, new TrustManager[] { tm }, null);
			SSLSocketFactory ssf = new SSLSocketFactory(ctx);
			ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			ClientConnectionManager ccm = base.getConnectionManager();
			SchemeRegistry sr = ccm.getSchemeRegistry();
			sr.register(new Scheme("https", ssf, 443));
			return new DefaultHttpClient(ccm, base.getParams());
		} catch (Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}
	
	
	/*
	 * main runner
	 * */
	public static void main (String[] args) throws Exception
	{
		System.out.println(UIPaymentInfoGetter.getPaymentDetailsString("5XT8185527704683N"));
	}

}
